1 00:00:00,560 --> 00:00:01,130 Hey there. 2 00:00:01,130 --> 00:00:03,050 Welcome back and thanks for joining me. 3 00:00:03,050 --> 00:00:07,820 We're going to continue where we left off with our wave system by adding sound effects, giving money 4 00:00:07,820 --> 00:00:13,580 to our players when they complete a wave, and highlighting the last alive zombie on the map inside 5 00:00:13,580 --> 00:00:19,340 of the workspace, I have a folder that was originally within our assets pack called sounds. 6 00:00:19,340 --> 00:00:23,570 Inside of here are going to be the different sounds I want to play when a wave is going on. 7 00:00:23,570 --> 00:00:28,820 For example, when we have a wave in progress, we're going to randomly pick one of these sound instances 8 00:00:28,820 --> 00:00:33,440 to play, such as this song or this song. 9 00:00:34,550 --> 00:00:39,680 We'll just randomly pick one of these sounds to play during a wave, and when we're in the intermission, 10 00:00:39,680 --> 00:00:41,300 we're going to be playing this intermission. 11 00:00:41,300 --> 00:00:42,080 Music. 12 00:00:44,450 --> 00:00:48,470 When the players win, a game will play this victory sound victory. 13 00:00:50,570 --> 00:00:55,340 And then when they fail or all the players die and they lose, we'll play this sound instead. 14 00:00:58,720 --> 00:01:03,100 And then at the start of every single round, before we start spawning zombies on the map, I want to 15 00:01:03,100 --> 00:01:04,960 go ahead and play this sound. 16 00:01:08,230 --> 00:01:13,510 So let's go ahead and implement these sounds as well as a couple of other things into our wave system. 17 00:01:14,800 --> 00:01:21,130 So inside of our game handler, what I'm going to do is I'm going to create a new section first to define 18 00:01:21,130 --> 00:01:25,660 the different kind of cash rewards I want to give out to players when they complete a wave. 19 00:01:25,660 --> 00:01:28,990 So I'm going to call this wave Cash Rewards. 20 00:01:28,990 --> 00:01:32,830 And then we can have a key for each wave and what the reward should be. 21 00:01:32,830 --> 00:01:38,140 For example, all the players that survive wave one will give them like 20 bucks for wave two. 22 00:01:38,140 --> 00:01:40,750 We'll also give them 20 bucks for wave three. 23 00:01:42,000 --> 00:01:48,720 We could do 20 bucks, wave four, wheel up it to 30 bucks for wave five. 24 00:01:49,020 --> 00:01:50,820 We'll set that to 30 as well. 25 00:01:50,820 --> 00:01:51,840 Wave six. 26 00:01:51,840 --> 00:01:54,060 We'll keep it at 30 and then wave seven. 27 00:01:54,060 --> 00:01:56,340 We're going to set that to 40. 28 00:01:56,340 --> 00:01:59,280 Wave eight we'll set to 50. 29 00:01:59,310 --> 00:02:01,950 Wave nine we'll set to 60. 30 00:02:01,950 --> 00:02:05,520 And then for wave ten we'll set that to 70. 31 00:02:05,550 --> 00:02:07,590 These are just some random values I'm sitting here. 32 00:02:07,590 --> 00:02:12,690 You can of course change these to whatever you'd like when you want to go and balance out your game. 33 00:02:12,690 --> 00:02:15,210 But I'm just going to leave them at these values for now. 34 00:02:15,210 --> 00:02:19,590 And I'm going to make some variables to refer to the different sounds that are in my workspace. 35 00:02:19,590 --> 00:02:23,520 So we'll get that sounds folder, which is just equal to workspace dot sounds. 36 00:02:23,520 --> 00:02:29,340 And then we're also going to get all of the possible music we can play for a wave. 37 00:02:29,340 --> 00:02:31,350 So we'll call this wave music. 38 00:02:31,350 --> 00:02:34,320 And that's equal to our sounds folder dot wave music. 39 00:02:34,320 --> 00:02:36,390 And we're going to get all the children in there. 40 00:02:36,510 --> 00:02:42,450 And then I want to keep track of what song is currently playing, whether that's the intermission music 41 00:02:42,450 --> 00:02:45,150 or if that's a song playing during a wave. 42 00:02:45,150 --> 00:02:47,760 We'll call it current playing music. 43 00:02:47,760 --> 00:02:53,370 And that's because when I need to fade out the music and switch it to the intermission. 44 00:02:53,370 --> 00:02:59,280 So, for example, let's say we're in a wave and we're killing zombies, and then we kill all the zombies, 45 00:02:59,280 --> 00:03:01,170 and then we switch to the intermission stage. 46 00:03:01,170 --> 00:03:06,840 Well, we want to fade out the music that was playing during the wave and fade it into the intermission 47 00:03:06,840 --> 00:03:10,500 music, so we can go ahead and create a dedicated function for doing that. 48 00:03:11,330 --> 00:03:13,730 I'll go down and create a new function. 49 00:03:13,730 --> 00:03:16,460 And I'm going to call this function fade sound. 50 00:03:16,460 --> 00:03:21,350 And we're just going to pass a sound instance to this function that we should either fade in or fade 51 00:03:21,350 --> 00:03:21,710 out. 52 00:03:21,710 --> 00:03:26,360 And we can check if we need to fade it in or fade it out if the sound is plain. 53 00:03:26,360 --> 00:03:31,310 So if the sound is plain, then that means we should stop playing the sound. 54 00:03:31,310 --> 00:03:33,380 But first we need to fade it out. 55 00:03:33,380 --> 00:03:40,370 So what I'm going to do is I'm going to use the tween service to fade the volume of my sound instance. 56 00:03:40,370 --> 00:03:42,620 So let's go ahead and get the tween service. 57 00:03:45,210 --> 00:03:51,300 And I also want to be able to keep track of the original volume of this sound instance that gets passed 58 00:03:51,300 --> 00:03:52,260 to the function. 59 00:03:52,290 --> 00:03:57,810 So first we're going to create our tween using the tween service create on our sound. 60 00:03:57,810 --> 00:04:00,930 We could do a tween info of like three seconds. 61 00:04:00,930 --> 00:04:03,660 And we want to set the volume equal to zero. 62 00:04:03,660 --> 00:04:08,760 Now before we start playing this tween we're going to go ahead and set an attribute on our sound instance. 63 00:04:08,760 --> 00:04:10,830 And we can call it original volume. 64 00:04:11,340 --> 00:04:14,130 And we're going to set it equal to the sound's original volume. 65 00:04:14,130 --> 00:04:15,900 That way we can always keep track of it. 66 00:04:16,350 --> 00:04:19,950 Then we can go ahead and play this fade tween. 67 00:04:20,610 --> 00:04:28,590 And then I also want this function to yield because let's say in the section where we're starting a 68 00:04:28,590 --> 00:04:31,200 wave, I want to play that howling sound. 69 00:04:31,200 --> 00:04:37,980 And before I start spawning zombies on the map, I want to wait for that howling sound to have fully 70 00:04:37,980 --> 00:04:39,120 faded in. 71 00:04:39,660 --> 00:04:47,370 So we're going to wait for the completed event for our tween to fire. 72 00:04:47,370 --> 00:04:48,870 So we'll call wait on it. 73 00:04:50,080 --> 00:04:52,750 And of course, this is for when we're fading out a sound. 74 00:04:52,750 --> 00:04:54,760 So when we're fading out, we're going to wait. 75 00:04:54,760 --> 00:04:58,810 And then once the event fires, then we can go ahead and stop playing the sound. 76 00:04:59,290 --> 00:05:05,050 Otherwise, if the sound is not playing, we need to fade it in and start playing it. 77 00:05:05,110 --> 00:05:10,690 So what I'm going to do is I'm going to check if the volume of the sound. 78 00:05:11,450 --> 00:05:13,040 Is not equal to zero. 79 00:05:13,100 --> 00:05:18,200 If it's not equal to zero, then we need to go ahead and set an original volume attribute on our sound. 80 00:05:18,200 --> 00:05:21,860 So sound set attribute original volume. 81 00:05:22,250 --> 00:05:25,130 We're going to set that to sound dot volume. 82 00:05:26,460 --> 00:05:30,360 And then we can go ahead and set the sound's volume equal to zero. 83 00:05:31,360 --> 00:05:33,610 And then we'll play the sound. 84 00:05:33,820 --> 00:05:36,880 And here we'll use the tween service once more. 85 00:05:36,910 --> 00:05:42,670 So I'll call this Fade Tween, and we'll create a new tween on the sound instance with a tween info 86 00:05:42,670 --> 00:05:44,380 dot new of three seconds. 87 00:05:44,440 --> 00:05:49,330 And we're going to fade the volume to be equal to the sound's original volume. 88 00:05:49,330 --> 00:05:55,090 So we'll do sound git attribute original volume. 89 00:05:57,140 --> 00:06:02,510 And then we can go ahead and play this tween, and then we'll wait for this tween to finish. 90 00:06:02,510 --> 00:06:06,080 So we'll do completed and then we'll yield right here. 91 00:06:07,520 --> 00:06:11,540 Now, for example, what we can do when we first start our game. 92 00:06:11,540 --> 00:06:18,980 We want to go ahead and start playing the intermission music that is in our workspace. 93 00:06:18,980 --> 00:06:27,260 So after we have told the players that we're waiting for someone to join the live team, after a player 94 00:06:27,260 --> 00:06:32,450 has joined the live team and we start the countdown, then we can go ahead and start playing the intermission 95 00:06:32,450 --> 00:06:32,960 music. 96 00:06:32,960 --> 00:06:37,100 So I'm going to set currently playing music equal to our sounds folder. 97 00:06:37,100 --> 00:06:38,060 Intermission. 98 00:06:38,240 --> 00:06:46,220 And then I don't want to yield here for the tween to finish fading in this intermission music. 99 00:06:46,220 --> 00:06:53,000 I want to go straight away to my countdown, so I'll just spawn my fade sound function in a new thread 100 00:06:53,000 --> 00:06:55,640 and we can just pass our fade sound function. 101 00:06:55,760 --> 00:06:59,810 And then the other argument we need to provide to our fade sound function is that sound instance. 102 00:06:59,810 --> 00:07:05,390 So we'll just pass current playing music and this will fade our intermission music to start playing. 103 00:07:05,870 --> 00:07:14,900 And then what we can go ahead and do is when we want to start a wave, we can play one of the howling 104 00:07:14,900 --> 00:07:16,430 sounds for the horde. 105 00:07:16,460 --> 00:07:25,130 So what I'm going to do is I'm going to use my game event, and I'm first going to fire to all my players 106 00:07:25,130 --> 00:07:32,030 that I want them to update some text on their screen, and I want to tell them that a wave is starting. 107 00:07:32,030 --> 00:07:36,350 So we'll say wave starting, and then we'll play our howling sound. 108 00:07:36,350 --> 00:07:40,790 So we'll do sounds folder dot horde howl will play that. 109 00:07:41,420 --> 00:07:45,650 And then we're going to fade the current playing music. 110 00:07:45,650 --> 00:07:50,750 So if we were in the intermission stage and now we need to go into the zombie fighting stage, then 111 00:07:50,750 --> 00:07:54,230 we're going to fade out the intermission music and we're going to wait for that. 112 00:07:54,680 --> 00:08:01,970 Then what we can go ahead and do is update our currently playing music to a new sound instance. 113 00:08:01,970 --> 00:08:07,040 So inside of our wave music folder, we can go ahead and pick a random one of those sound instances. 114 00:08:07,040 --> 00:08:11,240 So we'll do RNG next integer one to the length of wave music. 115 00:08:11,690 --> 00:08:17,300 And then we're going to fade that music in and we're going to not yield. 116 00:08:17,300 --> 00:08:23,180 So I'll spawn our fade sound function in a new thread and pass our currently playing music. 117 00:08:24,440 --> 00:08:29,840 So this will fade the intermission music out, and then we randomly pick a song to play for this wave, 118 00:08:29,840 --> 00:08:31,430 and then we start playing it. 119 00:08:32,380 --> 00:08:38,350 Then when we get placed inside an intermission, we need to go ahead and fade out the current playing 120 00:08:38,350 --> 00:08:38,830 music. 121 00:08:38,830 --> 00:08:45,010 So if we just came from a wave into the intermission, we need to fade out that wave music and then 122 00:08:45,010 --> 00:08:48,250 fade in the intermission music again. 123 00:08:48,490 --> 00:08:51,550 So what we can go ahead and do here is I'm going to not yield. 124 00:08:51,550 --> 00:08:55,540 So I'll spawn the fade sound function and a new thread. 125 00:08:55,540 --> 00:08:57,940 And we're going to fade out the current playing music. 126 00:08:57,940 --> 00:09:03,730 And then we're going to set current playing music equal to the sounds folder dot intermission. 127 00:09:03,730 --> 00:09:06,940 And then we're going to fade that sound in at the same time. 128 00:09:06,940 --> 00:09:11,440 So we'll do fade sound and then we'll pass current playing music. 129 00:09:12,040 --> 00:09:15,160 So this will fade the intermission music back into the game. 130 00:09:15,970 --> 00:09:23,290 And then what I want to do is when we lose the game, I want to fade out any music that's currently 131 00:09:23,290 --> 00:09:27,670 playing, whether it's the intermission music or it's the wave music, I want to completely fade it 132 00:09:27,670 --> 00:09:33,700 out and then I want to play either that failure sound or the victory sound that I showed you earlier. 133 00:09:34,120 --> 00:09:41,650 So when we lose the game, we want to fade out whatever current music that is playing. 134 00:09:41,650 --> 00:09:45,580 And then inside of our sounds folder, we want to play that failure sound. 135 00:09:46,190 --> 00:09:50,510 And then we can copy this and do the exact same thing in the game over win function. 136 00:09:50,510 --> 00:09:54,560 But this time we want to play our victory sound. 137 00:09:55,350 --> 00:10:00,540 And I believe that's all the sounds we're going to implement for our wave system. 138 00:10:00,570 --> 00:10:07,020 Now, the next thing I said I wanted to do is I wanted to award money to players when they complete 139 00:10:07,020 --> 00:10:08,550 a particular wave. 140 00:10:08,550 --> 00:10:12,930 So we're going to be using our wave cash rewards, and we're going to go through all the players and 141 00:10:12,930 --> 00:10:17,160 award them however much money for a particular wave they completed. 142 00:10:18,740 --> 00:10:26,690 What we're going to go ahead and do is we're going to our function that listens to when a zombie dies. 143 00:10:26,690 --> 00:10:33,530 And before we set the game state to the intermission state to start a new wave, we should go ahead 144 00:10:33,530 --> 00:10:38,270 and award all of our players for completing this particular wave. 145 00:10:38,270 --> 00:10:41,660 So let's go ahead and loop through every single player that's in our game. 146 00:10:41,660 --> 00:10:44,870 So we're going to do players, get players. 147 00:10:45,140 --> 00:10:51,440 And what we're going to do is we're going to refer to this player's leaderstats folder, get their money 148 00:10:51,440 --> 00:10:58,460 and then get the value and increment it by our wave cash rewards for the current wave that we're at. 149 00:10:58,460 --> 00:11:03,290 So we're going to pass wave and then index this with the current wave. 150 00:11:03,740 --> 00:11:07,610 So all of our players are going to get the money for the wave they completed. 151 00:11:08,580 --> 00:11:13,560 Now, the last thing I said I wanted to do is I wanted to create a highlight around the last zombie 152 00:11:13,560 --> 00:11:15,300 that's alive on our map. 153 00:11:15,300 --> 00:11:21,000 So if you know what a highlight instance is, if, for example, I add a highlight instance to this 154 00:11:21,000 --> 00:11:27,780 park bench, it highlights the park bench and you can see the park bench even through other parts or 155 00:11:27,780 --> 00:11:28,710 through other areas. 156 00:11:28,710 --> 00:11:34,620 So that way all the players on the map can see where the last zombie is located. 157 00:11:35,730 --> 00:11:40,620 So what we can go ahead and do is inside of our died event, inside of the function. 158 00:11:40,620 --> 00:11:48,810 Listening to that event, we can check if the number of zombies that are left is equal to one. 159 00:11:48,810 --> 00:11:52,590 Then we can go ahead and create a highlight on that last zombie. 160 00:11:52,590 --> 00:11:59,610 And the way we can do that is by looping through every single zombie that's inside of our zombies workspace 161 00:11:59,610 --> 00:12:00,210 folder. 162 00:12:00,210 --> 00:12:02,160 So we'll get all of the children in there. 163 00:12:03,530 --> 00:12:07,670 And we're going to try to find the zombie in this folder that is alive. 164 00:12:07,670 --> 00:12:10,760 So we can check this zombie that's in the folder. 165 00:12:10,760 --> 00:12:17,450 And if there humanoid's health is greater than zero, then that means yes, they are definitely alive. 166 00:12:17,450 --> 00:12:20,570 So we can go ahead and create a highlight over this zombie. 167 00:12:21,460 --> 00:12:24,760 So let's go ahead and create a function for doing that. 168 00:12:24,790 --> 00:12:26,770 I'll call this function. 169 00:12:27,580 --> 00:12:34,870 Create highlight and we'll get past the parent for this highlight as well as any properties we would 170 00:12:34,870 --> 00:12:37,060 like to manipulate for this highlight. 171 00:12:37,270 --> 00:12:42,670 And we'll create a new highlight instance that's equal to instance dot new highlight. 172 00:12:43,510 --> 00:12:47,890 And then for any of the properties that we get past here, we'll loop through every single property 173 00:12:47,890 --> 00:12:48,850 and value. 174 00:12:49,420 --> 00:12:53,680 In the properties and we'll apply that on our highlight. 175 00:12:56,520 --> 00:13:02,130 And then we can go ahead and set the parent of that highlight equal to that zombie and return highlight 176 00:13:02,130 --> 00:13:03,780 just in case we need it. 177 00:13:05,160 --> 00:13:07,920 Now we can go ahead and go back down. 178 00:13:08,340 --> 00:13:15,330 And when we find our live zombie, we can go ahead and call our create highlight function on this zombie. 179 00:13:15,330 --> 00:13:18,210 So the parent is going to be the zombie. 180 00:13:18,690 --> 00:13:23,160 And then one of the properties I want to manipulate is going to be the fill color property. 181 00:13:23,160 --> 00:13:26,730 I want to go ahead and change this to a different color from the default of red. 182 00:13:26,910 --> 00:13:32,250 And instead I'm going to pick like a yellowish orange type color, something like that. 183 00:13:33,250 --> 00:13:38,770 And then I'm actually going to merge these two into an if or else if statement. 184 00:13:38,770 --> 00:13:42,760 So if the amount of zombies left is equal to one, we'll do this. 185 00:13:42,760 --> 00:13:48,820 Otherwise, if the number of zombies left is equal to zero and we're still in the wave in progress state, 186 00:13:48,820 --> 00:13:51,490 then we'll go ahead and move to the next wave. 187 00:13:51,610 --> 00:13:54,640 So now our wave system should be totally complete. 188 00:13:54,640 --> 00:13:59,860 We should have sound effects playing like music, and we should highlight the last zombie that's alive. 189 00:13:59,860 --> 00:14:04,840 And we should also earn money from whenever we complete a wave. 190 00:14:05,400 --> 00:14:10,050 So to make this easier to test, I'm going to go ahead and decrease these intermission values. 191 00:14:10,050 --> 00:14:13,170 I'm just going to set them to five seconds so I don't have to wait as long. 192 00:14:13,170 --> 00:14:15,120 And then we can go and play test our game. 193 00:14:16,820 --> 00:14:18,050 We'll hit play here. 194 00:14:19,100 --> 00:14:19,460 Okay. 195 00:14:19,460 --> 00:14:22,340 Waves are starting in three, two, one. 196 00:14:22,340 --> 00:14:24,950 Let's go ahead and kill these zombies. 197 00:14:24,950 --> 00:14:25,880 And. 198 00:14:26,060 --> 00:14:26,330 Okay. 199 00:14:26,330 --> 00:14:26,780 Perfect. 200 00:14:26,780 --> 00:14:29,840 We had the horrid sound play. 201 00:14:29,990 --> 00:14:32,750 If you heard earlier, we also had our intermission music. 202 00:14:32,750 --> 00:14:37,490 And now we are having the wave music playing for this first wave. 203 00:14:37,490 --> 00:14:39,530 So let me go ahead and kill all these guys. 204 00:14:41,690 --> 00:14:43,640 Okay, we've got three zombies left. 205 00:14:43,640 --> 00:14:50,090 If I kill these first two, hopefully the last zombie should have a highlight applied around him. 206 00:14:50,090 --> 00:14:52,070 So once I kill this guy, there you go. 207 00:14:52,070 --> 00:14:56,630 As you can see, a highlight has been created around the last zombie in our game. 208 00:14:56,630 --> 00:15:02,360 So I'm still able to see the zombie even through these walls and that notifies all the players. 209 00:15:02,360 --> 00:15:03,890 Okay, the last zombie is here. 210 00:15:03,890 --> 00:15:04,850 Let's go kill him. 211 00:15:04,910 --> 00:15:09,260 And then when I kill this zombie, hopefully I should get awarded some extra money. 212 00:15:09,560 --> 00:15:10,250 There we go. 213 00:15:10,250 --> 00:15:16,880 So, as you can see, I got awarded a bunch of extra money for killing that zombie as well as for for 214 00:15:16,880 --> 00:15:18,710 the wave ending. 215 00:15:18,710 --> 00:15:20,450 And we already went through the intermission. 216 00:15:20,450 --> 00:15:26,750 But you did hear the intermission music and now we are in wave two and now I have 20 zombies to fight. 217 00:15:27,860 --> 00:15:30,710 And now a different wave music is playing. 218 00:15:31,820 --> 00:15:36,260 Now, if all of these zombies kill me, it should play that. 219 00:15:36,440 --> 00:15:36,650 Uh. 220 00:15:36,650 --> 00:15:37,400 We failed. 221 00:15:37,490 --> 00:15:38,060 There we go. 222 00:15:38,060 --> 00:15:38,900 We failed. 223 00:15:39,140 --> 00:15:40,220 Game over. 224 00:15:40,220 --> 00:15:43,550 Zombies win and I can't spawn into the map. 225 00:15:43,880 --> 00:15:46,610 And hopefully all these guys will get cleaned up. 226 00:15:47,180 --> 00:15:47,600 There we go. 227 00:15:47,600 --> 00:15:48,470 They all get deleted. 228 00:15:48,470 --> 00:15:50,060 We're cleaning up the game. 229 00:15:52,330 --> 00:15:55,060 Starting a new game in eight seconds. 230 00:15:55,060 --> 00:16:00,700 So, so far it seems that our wave loop system is working just fine. 231 00:16:02,180 --> 00:16:07,100 When I press this play button, it should start to play the intermission music as you heard. 232 00:16:07,100 --> 00:16:11,510 So let's go hop back in and there's the intermission music playing. 233 00:16:11,510 --> 00:16:15,230 And once this timer is up, it should play the zombie howl. 234 00:16:15,260 --> 00:16:16,190 There we go. 235 00:16:17,240 --> 00:16:20,900 And now we are playing the wave music. 236 00:16:21,290 --> 00:16:23,540 So everything is working well. 237 00:16:24,530 --> 00:16:25,190 All right. 238 00:16:25,190 --> 00:16:27,260 That's all I have for you in this lecture. 239 00:16:27,260 --> 00:16:29,450 And I'll see you in the next one.